/*
 * Copyright (C) 2014 luke.
 * 
 * This file is part of Silvie.
 * 
 * Silvie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Silvie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Silvie.  If not, see <http://www.gnu.org/licenses/>.
 */

package silvie.gui;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;

import silvie.Silvie;
import silvie.gui.filetree.FileTreeCellRenderer;
import silvie.gui.filetree.FileTreeModel;
import silvie.gui.filetree.FileTreeNode;

/**
 * Encapsulates a JFrame containing a JTree for browsing the Silver data
 * directory for resources.
 * 
 * @author luke
 */
public class FileExplorer {
	private static final String FRAME_TITLE = "File Explorer";
	private static final int SCROLLPANE_PREFERRED_WIDTH = 300;
	private static final int SCROLLPANE_PREFERRED_HEIGHT = 484;
	private static final int FRAME_PADDING = 4;
	private Silvie silvie;
	private JFrame explorerFrame;
	private JTree fileTree;

	/**
	 * FileExplorer constructor. It creates the Swing components and listeners.
	 * 
	 * @param silvie the controller to notify when a file is clicked in the
	 *        tree.
	 */
	public FileExplorer(Silvie silvie) {
		this.silvie = silvie;
		this.explorerFrame = new JFrame(FRAME_TITLE);
		this.explorerFrame.setIconImage(SilvieGUI.getCachedIcon(SilvieGUI.SILVER_ICON_PATH).getImage());
		this.explorerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.explorerFrame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// Cleanly exit
				silvie.closeCurrentPreviewer();
				silvie.unregisterCurrentWatcher();
			}
		});
		this.fileTree = new JTree();
		this.fileTree.setModel(null);
		this.fileTree.setCellRenderer(new FileTreeCellRenderer());
		this.fileTree.setBorder(new EmptyBorder(FRAME_PADDING, FRAME_PADDING, FRAME_PADDING, FRAME_PADDING));
		this.fileTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		JScrollPane fileTreeScrollPane = new JScrollPane(this.fileTree);
		fileTreeScrollPane.setPreferredSize(new Dimension(SCROLLPANE_PREFERRED_WIDTH, SCROLLPANE_PREFERRED_HEIGHT));
		this.fileTree.addTreeSelectionListener(e -> {
			FileTreeNode node = (FileTreeNode) this.fileTree.getLastSelectedPathComponent();
			if (node != null) {
				// Notify the controller
				this.silvie.preview(node.getFilepath(), node.getFormat());
			}
		});
		this.explorerFrame.getContentPane().add(fileTreeScrollPane);
		this.explorerFrame.pack();
	}

	public JFrame getFrame() {
		return this.explorerFrame;
	}

	public TreeModel getTreeModel() {
		return this.fileTree.getModel();
	}

	public void setFileTreeModel(FileTreeModel model) {
		this.fileTree.setModel(model);
	}
}
